home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / dynamic.c < prev    next >
Text File  |  1993-09-23  |  3KB  |  113 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        dynamic.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    November 25, 1991
  7. *
  8. *    defines methods for dynamic nested segment
  9. */
  10.  
  11. # include    "dynamic.h"
  12.  
  13. /******************************************************************
  14. *    initialize.
  15. *    Acceleration and velocity are initialized to zero.
  16. ******************************************************************/
  17. Dynamic_Segment::Dynamic_Segment(void)
  18. {
  19.     center = new Coord3;
  20.     
  21.     set_center(0.,0.,0.);
  22.     set_acceleration(0.,0.,0.);
  23.     set_velocity(0.,0.,0.);
  24. }
  25.  
  26. /******************************************************************
  27. *    set acceleration values
  28. ******************************************************************/
  29. void    Dynamic_Segment::set_acceleration(double x,double y,double z)
  30. {
  31.     ax = x;
  32.     ay = y;
  33.     az = z;
  34. }
  35.  
  36. /******************************************************************
  37. *    set velocity values
  38. ******************************************************************/
  39. void    Dynamic_Segment::set_velocity(double x,double y,double z)
  40. {
  41.     vx = x;
  42.     vy = y;
  43.     vz = z;
  44. }
  45.  
  46. /******************************************************************
  47. *    set center of segment
  48. ******************************************************************/
  49. void    Dynamic_Segment::set_center(double x,double y,double z)
  50. {
  51.     center->set(x,y,z);
  52. }
  53.  
  54. /******************************************************************
  55. *    get y coordinate of center of segment
  56. ******************************************************************/
  57. double    Dynamic_Segment::get_center_y(void)
  58. {
  59.     return center->y;
  60. }
  61.  
  62. /******************************************************************
  63. *    simulate - no need to override
  64. *    Note that in general we do NOT also 'simulate' all the nested
  65. *    segments here, since these segments may not themselves be
  66. *    dynamic.  (They just move along with the main segment.)
  67. *    Note smaller values of 'time' produce greater accuracy.
  68. *    Note we could allow acceleration and velocity to be
  69. *    general transformation matrices if we used matrix
  70. *    exponentiation (by 'time') instead of the approach used here.
  71. ******************************************************************/
  72. void    Dynamic_Segment::simulate(double time)
  73. {
  74.     Translation    *temp;
  75.     
  76.     vx = vx + ax * time;
  77.     vy = vy + ay * time;
  78.     vz = vz + az * time;
  79.     
  80.     temp = new Translation;
  81.     temp->set(vx * time,vy * time,vz*time);
  82.  
  83.     move(temp);    /* changes the segment's 'transformation' */
  84.     
  85.     delete temp;
  86. }
  87.  
  88. /******************************************************************
  89. *    move dynamic segment including center coordinate
  90. ******************************************************************/
  91. void    Dynamic_Segment::move(Transformation* trans)
  92. {
  93.     Coord3    *temp;
  94.  
  95.     Animated_Segment::move(trans);
  96.         
  97.     temp = new Coord3;
  98.     
  99.     temp->equate(center);
  100.     center->apply(temp,trans);
  101.  
  102.     delete temp;
  103. }
  104.  
  105. /******************************************************************
  106. *    destroy dynamic segment
  107. ******************************************************************/
  108. Dynamic_Segment::~Dynamic_Segment(void)
  109. {
  110.     delete center;
  111. }
  112.  
  113.